home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vmed.arc / VMIO.CCC < prev    next >
Text File  |  1985-12-03  |  4KB  |  146 lines

  1. /* VMIO/CCC
  2.  *    virtual memory add/move/delete/insert
  3.  *
  4.  *    Copyright 1983 by Jim Kyle - All Rights Reserved
  5.  *    Licensed for individual non-commercial use only.
  6.  *
  7.  *         created:    November 16, 1983 - Jim Kyle
  8.  *    last changed:    November 30, 1983 - Jim Kyle
  9.  */
  10.  
  11. /*
  12.  *    ins_btm() inserts a line in 'from' as the last line
  13.  *    of the current block and updates all block data. If
  14.  *    no room exists, a new block is added.
  15.  */
  16. ins_btm(from)    char *from;
  17. {    int len,sz;
  18.     t_r("ins_btm");
  19.     if ((len = getwd(from)) > BDSZ)
  20.         return(vm_err("ins_btm; bad input"));
  21.     sz = cur_size() + 1;
  22.     if ((sz + len) > BLKSIZ)
  23.         if (forcenew()) return(ERR);
  24.     return(cpy_in(from, eob(), len));
  25. }
  26.  
  27. /*
  28.  *    ins_top() inserts a line in 'from' as the first line
  29.  *    of the current block and updates all block data. If
  30.  *    no room exists, a new block is added.
  31.  */
  32. ins_top(from)    char *from;
  33. {    int len,sz;
  34.     t_r("ins_top");
  35.     if ((len = getwd(from)) > BDSZ)
  36.         return(vm_err("ins_top; bad input"));
  37.     sz = cur_size() + 1;
  38.     if ((sz + len) > BLKSIZ) {
  39.         if (go_pvb())
  40.             return(vm_err("ins_top prev"));
  41.         if (forcenew()) return(ERR);
  42.         }
  43.     else {
  44.         sz -= BHSZ;
  45.         vm_move(Dbptr,Dbptr+len,sz);
  46.         }
  47.     return(cpy_in(from,Dbptr,len));
  48. }
  49.  
  50. /*
  51.  *    ln_del() deletes the line pointed to, decrements
  52.  *    the last-line counts, and smudges the block. It
  53.  *    does NOT alter the current-line pointers.
  54.  */
  55. ln_del(p)    char *p;        /* delete line at *p */
  56. {    int n;
  57.     t_r("ln_del");
  58.     n = getwd(p);            /* length deleted */
  59.     vm_abt(n == 0,"ln_del, null count");
  60.     vm_move(p+n, p, eob()-p-n);     /* close up data */
  61.     *Bcptr -= n;            /* adjust byte count */
  62.     --Llcb;                    /* last line this block */
  63.     --Lltf;                    /* last line total file */
  64.     smudge();
  65.     --*Lcptr;                /* all done */
  66. }
  67.  
  68. mvln_nx()    /* move last line to top of next blk */
  69. {    int svcln;
  70.     char *p;
  71.     t_r("mvln_nx");
  72.     svcln = Cur_ln;        /* save current line nbr */;
  73.     p = llad();            /* save lastline pointer */
  74.     if (*Nxptr)             /* not at EOF */
  75.         go_nxb();        /*     so step to next */
  76.     else                /* at EOF */
  77.         forcenew();        /*     so force another block */
  78.     ins_top(p);            /* copy in the line */
  79.     go_pvb();            /* back to original block */
  80.     ln_del(p);            /* delete last line */
  81.     go_to(svcln);        /* and restore Cur_ln location */
  82. }
  83.  
  84. mvln_pv()    /* move top line to bottom of prev blk */
  85. {    int svcln;
  86.     char *p;
  87.     t_r("mvln_pv");
  88.     if (Curr == 0) return;        /* at front of file */
  89.     p = Dbptr;            /* save firstline pointer */
  90.     svcln = Cur_ln;        /*     and current-line nbr */
  91.     go_pvb();            /* make prev current */
  92.     ins_btm(p);            /* copy in the line */
  93.     while (Dbptr != p)
  94.         go_nxb();        /* back to original block */
  95.     ln_del(p);            /* delete first line */
  96.     go_to(svcln);        /* and restore pointers */
  97. }
  98.  
  99. /*                                                
  100.  *    forcenew() forces addition of a new block after the
  101.  *    current block and makes the new block current.
  102.  */
  103. forcenew()
  104. {    if (add_blk()) return(vm_err("forcenew add"));
  105.     if (vm_get(*Nxptr)) return(vm_err("forcenew nxt"));
  106.     Flcb = Llcb + 1;        /* Llcb does not change */
  107.     smudge();
  108.     return(OK);
  109. }
  110.  
  111. /*
  112.  *    cpy_in() copies 'len' bytes from 'src' to 'dst' and
  113.  *    updates pointers for the current block, assuming that
  114.  *    'dst' is within the current block's area.
  115.  */
  116. cpy_in(src,dst,len)    int len;    char *src,*dst;
  117. {    t_r("cpy_in entry");
  118.     *Bcptr += len;        /* current block byte count */
  119.     ++(*Lcptr);            /* current block line count */
  120.     ++Llcb;                /* current block last line */
  121.     ++Lltf;                /* total file line count */
  122.     vm_move(src,dst,len);
  123.     smudge();
  124.     t_r("cpy_in exit");
  125.     return(OK);
  126. }
  127.  
  128. /* 
  129.  *    open_hole() opens a hole in the current block 
  130.  *    for 'size' bytes, immediately before the current
  131.  *    line. If no room exists, enough lines are moved
  132.  *    to the next block to make room (inserting a new
  133.  *    next block if necessary).  Block pointers are
  134.  *    NOT adjusted; cpy_in() will fix them.
  135.  */
  136. open_hole(size)    int size; 
  137. {    vm_abt(size >= BDSZ, "open_hole");
  138.     t_r("open_hole entry");
  139.     while ((size + cur_size() + 1) >= BLKSIZ)
  140.         mvln_nx();        /* move enough to make hole */
  141.     vm_move(clad(),clad()+size,eob()-clad()+1);
  142.     putwd(size,clad());        /* keep size pointers OK */
  143.     t_r("open_hole exit");
  144. }
  145.  
  146.